﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using FastReflectionLib;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.QY.Entities;
using SZHL.Data;
using SZHL.UI.AppCode;

namespace SZHL.UI.API
{
    public class XMGLManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(XMGLManage).GetMethod(msg.Action.ToUpper());
            XMGLManage model = new XMGLManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }

        /// <summary>
        /// 添加请假数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="strParamData"></param>
        public void ADDXMGL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                ZH_XMGL xmgl = JsonConvert.DeserializeObject<ZH_XMGL>(P1);
                if (string.IsNullOrEmpty(xmgl.XMMC))
                {
                    msg.ErrorMsg = "项目名称不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(xmgl.YXJ))
                {
                    msg.ErrorMsg = "请选择优先级";
                    return;
                }
                if (string.IsNullOrEmpty(xmgl.XMFZR))
                {
                    msg.ErrorMsg = "请选择负责人";
                    return;
                }
                if (xmgl.ID == 0)
                {
                    xmgl.UpdateDate = DateTime.Now;
                    xmgl.CRDate = DateTime.Now;
                    xmgl.CRUser = UserInfo.User.UserName;
                    xmgl.CRUserName = UserInfo.User.UserRealName;
                    new ZH_XMGLB().Insert(xmgl);
                    //发送消息
                    //string content = UserInfo.User.UserRealName + "发起了项目";
                    //if (!string.IsNullOrEmpty(xmgl.XMFZR))
                    //{
                    //    new JH_Auth_User_CenterB().SendMsg(UserInfo, "xmgl", "项目管理", content, xmgl.ID, xmgl.XMFZR);
                    //}
                    //if (!string.IsNullOrEmpty(xmgl.XMCYR))
                    //{
                    //    new JH_Auth_User_CenterB().SendMsg(UserInfo, "xmgl", "项目管理", content, xmgl.ID, xmgl.XMCYR);
                    //}
                }
                else
                {
                    xmgl.UpdateDate = DateTime.Now;
                    xmgl.UpdateUser = UserInfo.User.UserName;
                    xmgl.UpdateUserName = UserInfo.User.UserRealName;
                    new ZH_XMGLB().Update(xmgl);
                }
                msg.Result = xmgl;

            }
            catch (Exception)
            {

                msg.ErrorMsg = "添加项目失败";
            }
        }

        /// <summary>
        /// 查看项目的详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETXMGLMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strFormID = P1;
            ZH_XMGL Model = new ZH_XMGLB().GetEntity(d => d.ID.ToString() == strFormID);
            msg.Result = Model;
            msg.Result1 = new JH_Auth_TLB().GetTL("XMGL", Model.ID.ToString());
            if (Model.Files != null && Model.Files.ToString() != "")
            {
                string fileIDs = Model.Files.ToString();
                List<FT_File> FileList = new FT_FileB().GetEntities(d => fileIDs.Split(',').Contains(d.ID.ToString())).ToList();
                msg.Result2 = new FT_FileB().GetEntities(d => fileIDs.Split(',').Contains(d.ID.ToString()));
            }
        }

        public void GETXMGLDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "  1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And Status='{0}'", P1);
            }
            if (P2 != "")
            {

                switch (P2)
                {
                    case "0":
                        strWhere += string.Format(" And (CRUser='{0}' or  ','+XMFZR+','  like '%,{0},%'  or ','+XMCYR+','  like '%,{0},%' )", UserInfo.User.UserName);
                        break;
                    case "1":
                        strWhere += string.Format(" And CRUser='{0}'", UserInfo.User.UserName);
                        break;
                    case "2":
                        strWhere += string.Format(" And (','+XMFZR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                    case "3":
                        strWhere += string.Format(" And (','+XMCYR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                }
            }
            string strContent = context.Request["Content"] ?? "";
            if (strContent != "")
            {
                strWhere += string.Format(" And ( XMMC like '%{0}%' )", strContent);
            }

            DataTable dtList = new DataTable();
            //dtList = new ZH_XMGLB().GetDTByCommand(" select *  FROM ZH_XMGL " + strWhere + " ORDER BY LeiBie DESC,CRDate DESC ");
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            dtList = new ZH_XTGGB().GetDataPager("ZH_XMGL", "* ", 8, page, "CRDate desc", strWhere, ref total);

            

            msg.Result = dtList;
            msg.Result1 = total;
        }
        public void GETXMGLDATA_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And Status={0}", P1);
            }
            if (P2 != "")
            {

                switch (P2)
                {
                    case "0":
                        strWhere += string.Format(" And (CRUser='{0}' or  ','+XMFZR+','  like '%,{0},%'  or ','+XMCYR+','  like '%,{0},%' )", UserInfo.User.UserName);
                        break;
                    case "1":
                        strWhere += string.Format(" And CRUser='{0}'", UserInfo.User.UserName);
                        break;
                    case "2":
                        strWhere += string.Format(" And (','+XMFZR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                    case "3":
                        strWhere += string.Format(" And (','+XMCYR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                }
            }
            DataTable dtList = new DataTable();
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            dtList = new ZH_XTGGB().GetDataPager("ZH_XMGL", "* ", 8, page, "CRDate desc", strWhere, ref total);

            dtList.Columns.Add("PLList", Type.GetType("System.Object"));
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                dtList.Rows[i]["PLList"] = new JH_Auth_TLB().GetTL("RCTX", dtList.Rows[i]["ID"].ToString());
            }
            msg.Result = dtList;

        }

        public void DELXMGL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            new ZH_XMGLB().Delete(D => D.ID.ToString() == P1);
        }

    }
}